<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>京东 - 注册</title>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        /* 顶部导航 */
        .top-nav {
            background-color: #e3e4e5;
            height: 30px;
            line-height: 30px;
            font-size: 12px;
            color: #999;
        }
        
        .top-nav-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
        }
        
        .top-nav-left, .top-nav-right {
            display: flex;
            list-style: none;
        }
        
        .top-nav-item {
            margin: 0 10px;
        }
        
        .top-nav-item a {
            color: #999;
            text-decoration: none;
        }
        
        .top-nav-item a:hover {
            color: #e4393c;
        }
        
        /* 头部 */
        .header {
            background-color: #fff;
            padding: 20px 0;
        }
        
        .header-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            align-items: center;
        }
        
        .logo img {
            height: 60px;
        }
        
        /* 注册容器 */
        .register-container {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 50px 0;
        }
        
        .register-card {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            width: 400px;
            padding: 40px;
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .register-title {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .register-title h2 {
            color: #333;
            font-size: 24px;
            font-weight: 500;
        }
        
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }
        
        .form-input {
            width: 100%;
            height: 40px;
            padding: 0 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #2878ff;
        }
        
        .form-input-icon {
            position: absolute;
            right: 12px;
            top: 36px;
            color: #999;
        }
        
        .register-button {
            width: 100%;
            height: 40px;
            background-color: #2878ff;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .register-button:hover {
            background-color: #1a64e6;
        }
        
        .login-link {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
        }
        
        .login-link a {
            color: #2878ff;
            text-decoration: none;
        }
        
        .login-link a:hover {
            text-decoration: underline;
        }
        
        /* 底部 */
        .footer {
            background-color: #fff;
            padding: 20px 0;
            border-top: 1px solid #ddd;
        }
        
        .footer-container {
            max-width: 1200px;
            margin: 0 auto;
            text-align: center;
            color: #666;
            font-size: 12px;
        }
        
        /* 弹窗样式 */
        .popup {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            width: 300px;
            z-index: 1000;
            animation: fadeInPopup 0.3s ease-out;
        }
        
        @keyframes fadeInPopup {
            from { opacity: 0; transform: translate(-50%, -60%); }
            to { opacity: 1; transform: translate(-50%, -50%); }
        }
        
        .popup-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .popup-title {
            font-size: 16px;
            font-weight: 500;
            color: #333;
        }
        
        .popup-close {
            font-size: 20px;
            color: #999;
            cursor: pointer;
        }
        
        .popup-content {
            color: #666;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .popup-actions {
            display: flex;
            justify-content: center;
        }
        
        .popup-button {
            padding: 8px 20px;
            background-color: #2878ff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .popup-button:hover {
            background-color: #1a64e6;
        }
        
        .popup-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.3);
            z-index: 999;
        }
        
        /* 成功提示样式 */
        .success-toast {
            display: none;
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #52c41a;
            color: #fff;
            padding: 10px 20px;
            border-radius: 4px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            z-index: 1000;
            animation: fadeInToast 0.3s ease-out;
        }
        
        @keyframes fadeInToast {
            from { opacity: 0; transform: translate(-50%, -10px); }
            to { opacity: 1; transform: translate(-50%, 0); }
        }
        
        /* 响应式 */
        @media (max-width: 500px) {
            .register-card {
                width: 90%;
                padding: 30px;
            }
            
            .top-nav {
                display: none;
            }
            
            .logo img {
                height: 40px;
            }
            
            .popup {
                width: 80%;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航 -->
    <div class="top-nav">
        <div class="top-nav-container">
            <ul class="top-nav-left">
                <li class="top-nav-item"><a href="#">北京</a></li>
            </ul>
            <ul class="top-nav-right">
                <li class="top-nav-item"><a href="login.html">你好，请登录</a></li>
                <li class="top-nav-item"><a href="#">免费注册</a></li>
                <li class="top-nav-item"><a href="#">我的订单</a></li>
                <li class="top-nav-item"><a href="#">我的京东</a></li>
                <li class="top-nav-item"><a href="#">客户服务</a></li>
            </ul>
        </div>
    </div>
    
    <!-- 头部 -->
    <header class="header">
        <div class="header-container">
            <div class="logo">
                <a href="index.html"><img src="https://picsum.photos/id/237/200/60" alt="京东Logo"></a>
            </div>
        </div>
    </header>
    
    <!-- 注册容器 -->
    <main class="register-container">
        <div class="register-card">
            <div class="register-title">
                <h2>用户注册</h2>
            </div>
            
            <form id="registerForm">
                <div class="form-group">
                    <label for="username">用户名 <span style="color: #e4393c;">*</span></label>
                    <div class="input-group">
                        <i class="fa fa-user form-input-icon"></i>
                        <input type="text" id="username" name="username" class="form-input" placeholder="请设置6-20位用户名" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="password">密码 <span style="color: #e4393c;">*</span></label>
                    <div class="input-group">
                        <i class="fa fa-lock form-input-icon"></i>
                        <input type="password" id="password" name="password" class="form-input" placeholder="请设置6-20位密码" required>
                    </div>
                    <p class="password-tip" style="font-size: 12px; color: #999; margin-top: 5px;">密码至少6位，建议使用字母、数字和符号组合</p>
                </div>
                
                <div class="form-group agree-terms">
                    <input type="checkbox" id="agree" name="agree" required style="vertical-align: middle; margin-right: 5px;">
                    <label for="agree" style="font-size: 12px; color: #666;">我已阅读并同意<a href="#" style="color: #2878ff; text-decoration: underline;">《用户协议》</a>和<a href="#" style="color: #2878ff; text-decoration: underline;">《隐私政策》</a></label>
                </div>
                
                <div class="form-group">
                    <input type="submit" value="立即注册" class="register-button">
                </div>
            </form>
            <div class="login-link">
                <p>已有账号？<a href="login.html">立即登录</a></p>
            </div>
        </div>
    </main>
    
    <!-- 底部 -->
    <footer class="footer">
        <div class="footer-container">
            <div class="footer-links">
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">关于我们</a>
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">联系我们</a>
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">网站地图</a>
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">隐私政策</a>
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">京东社区</a>
                <a href="#" style="color: #666; text-decoration: none; margin: 0 10px;">诚聘英才</a>
            </div>
            <p style="margin-top: 10px;">Copyright © 2004-2023 京东JD.com 版权所有 | 京ICP证070359号</p>
        </div>
    </footer>
    
    <!-- 弹窗元素 -->
    <div id="errorPopup" class="popup">
        <div class="popup-header">
            <div class="popup-title">注册失败</div>
            <div class="popup-close" onclick="closePopup()">&times;</div>
        </div>
        <div class="popup-content" id="popupMessage"></div>
        <div class="popup-actions">
            <button class="popup-button" onclick="closePopup()">确定</button>
        </div>
    </div>
    
    <div id="popupOverlay" class="popup-overlay" onclick="closePopup()"></div>
    
    <!-- 成功提示 -->
    <div id="successToast" class="success-toast">
        注册成功！
    </div>
    
    <script>
        // 密码强度检测
        const passwordInput = document.getElementById('password');
        const passwordTip = document.querySelector('.password-tip');
        
        passwordInput.addEventListener('input', function() {
            const password = this.value;
            let strength = 0;
            
            // 检测密码强度
            if (password.length >= 6) strength++;
            if (password.length >= 10) strength++;
            if (/[A-Z]/.test(password)) strength++;
            if (/[a-z]/.test(password)) strength++;
            if (/[0-9]/.test(password)) strength++;
            if (/[^A-Za-z0-9]/.test(password)) strength++;
            
            // 更新提示颜色
            if (strength <= 2) {
                passwordTip.style.color = '#e4393c';
                passwordTip.innerHTML = '密码强度：弱，建议使用字母、数字和符号组合';
            } else if (strength <= 4) {
                passwordTip.style.color = '#ff9800';
                passwordTip.innerHTML = '密码强度：中，建议使用字母、数字和符号组合';
            } else {
                passwordTip.style.color = '#4caf50';
                passwordTip.innerHTML = '密码强度：强，安全性较高';
            }
        });
        
        // 显示错误弹窗
        function showErrorPopup(message) {
            document.getElementById('popupMessage').textContent = message;
            document.getElementById('errorPopup').style.display = 'block';
            document.getElementById('popupOverlay').style.display = 'block';
        }
        
        // 关闭弹窗
        function closePopup() {
            document.getElementById('errorPopup').style.display = 'none';
            document.getElementById('popupOverlay').style.display = 'none';
        }
        
        // 显示成功提示
        function showSuccessToast() {
            const toast = document.getElementById('successToast');
            toast.style.display = 'block';
            
            // 3秒后自动隐藏
            setTimeout(() => {
                toast.style.display = 'none';
            }, 3000);
        }
        
        // 表单提交处理
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault(); // 阻止默认提交行为
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const agree = document.getElementById('agree').checked;
            
            // 前端表单验证
            if (!username) {
                showErrorPopup('请设置用户名');
                return;
            }
            
            if (!password) {
                showErrorPopup('请设置密码');
                return;
            }
            
            if (password.length < 6) {
                showErrorPopup('密码长度不能少于6位');
                return;
            }
            
            if (!agree) {
                showErrorPopup('请同意用户协议和隐私政策');
                return;
            }
            
            // 禁用提交按钮，防止重复提交
            const submitButton = this.querySelector('input[type="submit"]');
            submitButton.disabled = true;
            submitButton.value = '注册中...';
            
            // 准备表单数据
            const formData = new FormData(this);
            
            // 发送AJAX请求到后端
            fetch('php/register.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                // 恢复提交按钮状态
                submitButton.disabled = false;
                submitButton.value = '立即注册';
                
                if (data.status) {
                    // 注册成功
                    showSuccessToast();
                    
                    // 重置表单
                    this.reset();
                    
                    // 3秒后跳转到登录页
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 3000);
                } else {
                    // 注册失败，显示错误信息
                    showErrorPopup(data.message);
                }
            })
            .catch(error => {
                // 恢复提交按钮状态
                submitButton.disabled = false;
                submitButton.value = '立即注册';
                
                // 网络错误
                showErrorPopup('网络错误，请稍后再试');
                console.error('注册请求失败:', error);
            });
        });
    </script>
</body>
</html>
